package com.yyw.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.Service.UserService;
import com.yyw.bean.User;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "用户API")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${my.session.user}")
    private String userAttr;

    @CrossOrigin
    @ApiOperation(value = "退出登录", httpMethod = "GET")
    @GetMapping("/user/exit")
    public Result exitLogin(@ApiIgnore HttpSession session) {
        session.removeAttribute(userAttr);
        return ResultFactory.success("成功退出");
    }

    @CrossOrigin
    @ApiOperation(value = "退出当前班级", httpMethod = "GET")
    @GetMapping("/user/exitClass")
    public Result exitClass() {
        User user = MyContext.getSessionUser();
        user.setClassid(null);
        user.setClassname(null);
        return userService.updateUserClassInfo(user);
    }

    @CrossOrigin
    @ApiOperation(value = "修改用户信息", httpMethod = "POST")
    @PostMapping("/user/updateUser")
    public Result updateUser(@RequestBody User user) {
        user.setId(MyContext.getSessionUser().getId());
        return userService.updateUser(user);
    }

    @CrossOrigin
    @ApiOperation(value = "获取登录信息", httpMethod = "GET")
    @GetMapping("/user/user")
    public Result getUser(@ApiIgnore HttpSession session) {
        return ResultFactory.success(session.getAttribute(userAttr));
    }

    @CrossOrigin
    @ApiOperation(value = "用户登录", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "tel", value = "手机号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true) })
    @PostMapping("/login")
    public Result login(@RequestParam("tel") String tel, @RequestParam("password") String password) {

        return userService.login(tel, password);
    }

    @CrossOrigin
    @ApiOperation(value = "用户注册", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "type", value = "用户类型", required = true),
            @ApiImplicitParam(name = "tel", value = "手机号", required = true) })
    @PostMapping("/register")
    public Result register(@RequestParam("name") String name, @RequestParam("password") String password,
            @RequestParam("type") Integer type, @RequestParam("tel") String tel) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setType(type);
        user.setTel(tel);
        log.info("/register:" + user.toString());
        return userService.register(user);
    }
}
